package com.example.demo.dao;

import com.example.demo.pojo.User;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

//@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) {
        String sql = "insert into user(user_name,password) values(?,?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from user where user_id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int update(User user) {
        String sql = "update user set user_name=?, password=? where user_id=?";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getId());
    }

    @Override
    public User get(Integer id) {
        String sql = "select * from user where user_id=?";
        return jdbcTemplate.queryForObject(sql, new User(), id);
    }

    @Override
    public List<User> getAll() {
        String sql = "select * from user";
        return jdbcTemplate.query(sql, new User());
    }

    @Override
    public List<User> getByName(String username) {
        return null;
    }

    @Override
    public Page<User> getByName2(String username) {
        return null;
    }
}
